package edu.lingnan.house.controller;

import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;
import edu.lingnan.house.service.RentHouseService;
import edu.lingnan.house.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.*;

@Controller
@RequestMapping("/rent")
public class RentHouseController {
    @Autowired
    private RentHouseService rentHouseService;
    //显示租房页面
    @GetMapping("/selectRentHouse")
    @ResponseBody
    public Result selectRentHouse(Integer minArea,Integer maxArea,Double minPrice,Double maxPrice,String address,Integer roomSum,Integer sellType){
        return rentHouseService.selectRentHouse(minArea, maxArea, minPrice, maxPrice, address,roomSum,sellType);
    }
    //获取电脑ip
    @GetMapping("/getIp")
    @ResponseBody
    public Result getIp() throws Exception {
        String getIp="http://124.222.230.19/ip";
        String str="";
        try {

            URL url = new URL(getIp);

            InputStream in = url.openStream();

            InputStreamReader isr = new InputStreamReader(in);

            BufferedReader bufr = new BufferedReader(isr);
            String my;
            while ((my = bufr.readLine()) != null) {

                str+=my;

            }

            bufr.close();

            isr.close();

            in.close();


        } catch (Exception e) {

            e.printStackTrace();

        }finally {
            return getIpName(str);
        }
    }


    //获取返回的地址document.getElementById('showIpInfo').innerHTML='广东省广州市 电信';
    private Result getIpName(String tureIp) throws Exception {

        String str="";
        String result;
        String pro;
        try {
            tureIp="http://whois.pconline.com.cn/jsLabel.jsp?ip="+tureIp+"&id=showIpInfo";
            URL url = new URL(tureIp);

            InputStream in = url.openStream();

            InputStreamReader isr = new InputStreamReader(in);

            BufferedReader bufr = new BufferedReader(isr);
            String my;


            while ((my = bufr.readLine()) != null) {

                str+=my;

            }

            bufr.close();

            isr.close();

            in.close();


        } catch (Exception e) {

            e.printStackTrace();

        }finally {
            System.out.println("str" + str);
            result= str.split("='")[1];
            System.out.println("re" + result);
            pro = str.split("省")[0];
            System.out.println("22" + pro );
            result = str.split("省")[1];
            List<String>results = Arrays.asList(result.split("市"));
            if (results.size()==0){
                result =result.split("地区")[0];
            }else {
                result = results.get(0);
            }

            HashMap<String, String> map = new HashMap<>();
            map.put("pro",pro);
            map.put("result",result);
            System.out.println("33" + result );
            return new Result(200,map,"成功");
        }
    }

}
